package com.hxzy.projectssmplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hxzy.projectssmplus.dto.SysRoleDTO;
import com.hxzy.projectssmplus.entity.SysAdminUser;
import com.hxzy.projectssmplus.entity.SysAdminUserRole;
import com.hxzy.projectssmplus.entity.SysRole;
import com.hxzy.projectssmplus.service.SysAdminUserService;
import com.hxzy.projectssmplus.service.SysRoleService;
import com.hxzy.projectssmplus.vo.AjaxResult;
import com.hxzy.projectssmplus.vo.AjaxTableInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RoleController extends BaseController {


    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysAdminUserService sysAdminUserService;

    /**
     * 查询角色列表
     * @param sysRoleDTO
     * @return
     */
    @PreAuthorize("hasAuthority('system:role:list')")
    @GetMapping(value = "/role/data")
    public AjaxTableInfo  ajaxData(SysRoleDTO sysRoleDTO){
         IPage<SysRole> page = this.sysRoleService.searchByXML(super.startPage(), sysRoleDTO);
         return super.getTableInfo(page);
    }

    /**
     * 新增角色
     * @param sysRole
     * @return
     */
    @PreAuthorize("hasAuthority('system:role:add')")
    @PostMapping(value = "/role/add")
    public AjaxResult add(@RequestBody SysRole sysRole) {
        int insertRole = this.sysRoleService.insertRole(sysRole);
        if (insertRole > 0) {
            return AjaxResult.success("新增成功");
        }else {
            return AjaxResult.error("新增失败");
        }
    }

    /**
     * 修改角色
     * @param sysRole
     * @return
     */
    @PreAuthorize("hasAuthority('system:role:edit')")
    @PostMapping(value = "/role/edit")
    public AjaxResult edit(@RequestBody SysRole sysRole) {
        boolean updateById = this.sysRoleService.updateById(sysRole);
        if (updateById) {
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 修改保存数据权限
     * @param role
     * @return
     */
    @PreAuthorize("hasAuthority('system:role:edit')")
    @PostMapping(value = "/role/dataScope")
    public AjaxResult dataScope(@RequestBody SysRole role) {
        int update = this.sysRoleService.authDataScope(role);
        if (update > 0) {
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除角色
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('system:role:remove')")
    @PostMapping(value = "/role/remove/{roleId}")
    public AjaxResult remove(@PathVariable(value = "roleId") Long id) {
        int deleteRoleById = this.sysRoleService.deleteRoleById(id);
        if (deleteRoleById > 0) {
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败");
        }
    }

    /**
     * 根据角色id查询角色详细信息
     * @param roleId
     * @return
     */
    @GetMapping(value = "/role/detail/{roleId}")
    public AjaxResult getRoleDetail(@PathVariable(value = "roleId") Long roleId) {
        SysRole sysRole = this.sysRoleService.selectRoleById(roleId);
        return AjaxResult.success_data(sysRole);
    }

    /**
     * 更新表格中的状态
     * @param sysRole
     * @return
     */
    @PreAuthorize("hasAuthority('system:role:edit')")
    @PostMapping(value = "/role/changeStatus")
    public AjaxResult changeStatus(@RequestBody SysRole sysRole) {
        boolean updateById = this.sysRoleService.updateById(sysRole);
        if (updateById) {
            return AjaxResult.success("修改成功");
        }else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 分页查询已分配用户角色列表
     * @param sysAdminUser
     * @return
     */
    @GetMapping(value = "/role/allocatedList")
    public AjaxTableInfo allocatedList(SysAdminUser sysAdminUser) {
        IPage<SysAdminUser> sysAdminUserIPage = this.sysAdminUserService.selectAllocatedList(super.startPage(), sysAdminUser);
        return super.getTableInfo(sysAdminUserIPage);
    }

    /**
     * 分页查询未分配用户角色列表
     * @param sysAdminUser
     * @return
     */
    @GetMapping(value = "/role/unallocatedList")
    public AjaxTableInfo unallocatedList(SysAdminUser sysAdminUser) {
        IPage<SysAdminUser> sysAdminUserIPage = this.sysAdminUserService.selectUnallocatedList(super.startPage(), sysAdminUser);
        return super.getTableInfo(sysAdminUserIPage);
    }

    /**
     * 批量选择用户授权
     * @param roleId
     * @param userIds
     * @return
     */
    @PreAuthorize("hasAuthority('system:role:edit')")
    @PostMapping(value = "/role/authUser/selectAll")
    public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds) {
        int i = this.sysRoleService.insertAuthUsers(roleId, userIds);
        if (i > 0) {
            return AjaxResult.success("授权成功");
        }else {
            return AjaxResult.error("授权失败");
        }
    }

    /**
     * 批量取消授权用户
     * @param roleId
     * @param userIds
     * @return
     */
    @PostMapping(value = "/role/authUser/cancelAll")
    public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds) {
        int i = this.sysRoleService.deleteAuthUsers(roleId, userIds);
        if (i > 0) {
            return AjaxResult.success("取消授权成功");
        }else {
            return AjaxResult.error("取消授权失败");
        }
    }

    /**
     * 取消用户授权
     * @return
     */
    @PostMapping(value = "/role/authUser/cancel")
    public AjaxResult cancelAuthUser(SysAdminUserRole sysAdminUserRole) {
        int i = this.sysRoleService.deleteAuthUser(sysAdminUserRole);
        if (i > 0) {
            return AjaxResult.success("取消授权成功");
        }else {
            return AjaxResult.error("取消授权失败");
        }
    }


}
